package com.ruoyi.system.service.impl;

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.SysSaleRecords;
import com.ruoyi.system.mapper.SysSaleRecordsMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.system.domain.SysItemCategory;
import com.ruoyi.system.mapper.SysItemMapper;
import com.ruoyi.system.domain.SysItem;
import com.ruoyi.system.service.ISysItemService;

/**
 * 商品Service业务层处理
 *
 * @author silencezheng
 * @date 2022-10-29
 */
@Service
public class SysItemServiceImpl implements ISysItemService
{
    @Autowired
    private SysItemMapper sysItemMapper;
    @Autowired
    private SysSaleRecordsMapper saleRecordsMapper;

    /**
     * 查询商品
     *
     * @param id 商品主键
     * @return 商品
     */
    @Override
    public SysItem selectSysItemById(Long id)
    {
        return sysItemMapper.selectSysItemById(id);
    }

    /**
     * 查询商品列表
     *
     * @param sysItem 商品
     * @return 商品
     */
    @Override
    public List<SysItem> selectSysItemList(SysItem sysItem)
    {
        return sysItemMapper.selectSysItemList(sysItem);
    }

    /**
     * 新增商品
     *
     * @param sysItem 商品
     * @return 结果
     */
    @Transactional
    @Override
    public int insertSysItem(SysItem sysItem)
    {
        sysItem.setCreateTime(DateUtils.getNowDate());
        int rows = sysItemMapper.insertSysItem(sysItem);
        return rows;
    }

    /**
     * 修改商品
     *
     * @param sysItem 商品
     * @return 结果
     */
    @Override
    public int updateSysItem(SysItem sysItem)
    {
        sysItem.setUpdateTime(DateUtils.getNowDate());
        return sysItemMapper.updateSysItem(sysItem);
    }

    /**
     * 批量删除商品
     *
     * @param ids 需要删除的商品主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteSysItemByIds(Long[] ids)
    {
        sysItemMapper.deleteSysItemCategoryByIds(ids);
        return sysItemMapper.deleteSysItemByIds(ids);
    }

    /**
     * 删除商品信息
     *
     * @param id 商品主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteSysItemById(Long id)
    {
        sysItemMapper.deleteSysItemCategoryById(id);
        return sysItemMapper.deleteSysItemById(id);
    }
    /**
     * 购买商品信息
     *
     * @param ids 商品主键
     * @return 结果
     */
    @Transactional
    @Override
    public int buySysItem(Long[] ids) {
        //更改商品库存及销售数量
        for (Long id : ids) {
            sysItemMapper.buySysItem(id);
            //添加销售记录
            SysSaleRecords records = new SysSaleRecords();
            records.setItemId(id);
            records.setCreateTime(DateUtils.getNowDate());
            records.setCreateBy("admin");
            saleRecordsMapper.insertSysSaleRecords(records);
        }
        return ids.length;
    }

}
